import os

import requests

from apis.basepage import Basepage
from apis.loginApi import Login
from common.config import ip
from common.domy import DoDb
from config.conf import data_path


class Logdata(Basepage):
    # header = {"Admin-Token":""}
    def addfile(self,fname):
        fData1 = {"type": "img"}
        fileName=os.path.join(data_path,fname)
        FData2 = {'file':
                      (fname,
                       open(fileName, 'rb'),
                       'image/png')}
        c3 = requests.post(f"{ip}/file/upload", data=fData1, headers=self.header, files=FData2)
        print(c3.json())
        bid = c3.json().get("batchId")  # 提取 batchId
        return bid
    def addlog(self,xinlogdata):

        # xinlogdata = {"categoryId": "1", "content": "这里是今日内容~~", "tomorrow": "这里明日工作计划~",
        #               "question": "这里是今日遇到的问题", "batchId": bid, "sendUserIds": "3,4"}
        c4 = requests.post(f"{ip}/OaLog/addOrUpdate", json=xinlogdata, headers=self.header)
        print(c4.json())
        return c4  # 新增：返回响应对象，供测试代码获取结果
    def logdele(self,dellogdate):
        c5 = requests.post(f"{ip}/OaLog/deleteById", data=dellogdate, headers=self.header)
        print(c5.json())
        return c5
    # 获取日志
    def getlogdata(self,searchLogData={"page":1,"limit":5,"by":"1"}):
        c6 = requests.post(f"{ip}/OaLog/queryList", json=searchLogData, headers=self.header)
        return  c6
    #修改日志
    def xiulogdata(self,xiulogData):
        c7 = requests.post(f"{ip}/OaLog/addOrUpdate", json=xiulogData, headers=self.header)
        print(c7.json())
        return c7
    #删除全部日志
    def clearlog(self):
        r=self.getlogdata()
        c=r.json()["data"]["list"]
        cd=[]
        for i in c:
            # cd.append("logId")
            dl={"logId":i["logId"]}
            self.logdele(dl)
    def getlogidsBySQL(self,sql="SELECT log_id FROM 72crm_oa_log WHERE create_user_id = (SELECT user_id FROM 72crm_admin_user WHERE username = 'admin')"):
        try:
            a = DoDb()
            c = a.domysq(sql)
            b=[]
            for i in c:
                b.append(i[0])
            return b
        except Exception as e:
             print(f"发生了异常异常的信息是{e}")

if __name__ == '__main__':
        r = Login().login()
        token=r.json()["Admin-Token"]
        log= Logdata(token)
        # bid = log.addfile()
        # log.addlog(bid)
        a=log.getlogidsBySQL()
        print(a)
